{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import and Exporting Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.mlab as ml\n",
    "import numpy as np\n",
    "import openpyxl\n",
    "import scipy.io as io\n",
    "import xlrd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This exercise is done in Excel or OpenOffice"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import1 = np.genfromtxt(\"exercise3_numeric.csv\", delimiter=\",\")\n",
    "import2 = np.loadtxt(\"exercise3_numeric.csv\", delimiter=\",\", skiprows=1)\n",
    "import4 = np.genfromtxt(\"exercise3_numeric.txt\", delimiter=\"\\t\")\n",
    "import5 = np.loadtxt(\"exercise3_numeric.txt\", delimiter=\"\\t\", skiprows=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dates = import2[:, 0]\n",
    "SP500 = import2[:, 1]\n",
    "XOM = import2[:, 2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 4\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savez(\"exercise3\", dates=dates, SP500=SP500, XOM=XOM)\n",
    "np.savez_compressed(\"exercise3_compressed\", dates=dates, SP500=SP500, XOM=XOM)\n",
    "mdict = {\"dates\": dates, \"SP500\": SP500, \"XOM\": XOM}\n",
    "io.savemat(\"exercise3\", mdict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 5\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sumreturns = SP500 + XOM\n",
    "outputdata = np.hstack((dates, sumreturns))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 6\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt(\"exercise3_new.csv\", outputdata)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 7\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wb = xlrd.open_workbook(\"exercise3.xls\")\n",
    "sheetNames = wb.sheet_names()\n",
    "# Assumes 1 sheet name\n",
    "sheet = wb.sheet_by_name(sheetNames[0])\n",
    "excel_data = []\n",
    "for i in range(sheet.nrows):\n",
    "    excel_data.append(sheet.row_values(i))\n",
    "header = excel_data[0]\n",
    "import_excel = np.array(excel_data[1:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercise 8\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wb = openpyxl.load_workbook(\"exercise3.xlsx\")\n",
    "sheet = wb[wb.sheetnames[0]]\n",
    "excel_data = []\n",
    "rows = list(sheet.rows)\n",
    "header = []\n",
    "for c in rows[0]:\n",
    "    header.append(c.value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    },
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for r in rows[1:]:\n",
    "    data = []\n",
    "    for c in r:\n",
    "        data.append(c.value)\n",
    "    excel_data.append(data)\n",
    "import_xlsx = np.array(excel_data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
